//
//  MyInforChangeDetailViewController.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 17/5/5.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "MyInforChangeDetailViewController.h"
#import "LGetCodeView.h"
#import "CManager.h"
@interface MyInforChangeDetailViewController ()<GetCodeDelegate>
/**  新手机号  */
@property (strong, nonatomic) LGetCodeView  *phoneNumber;
/**  验证码  */
@property (strong, nonatomic) LGetCodeView  *code;
/**  手机号  */
@property (strong, nonatomic) UILabel  *phoneLabel;
/**  验证码  */
@property (strong, nonatomic) UILabel  *codeLabel;

/**  确认按钮  */
@property (strong, nonatomic) UIButton  *sureButton;


@end

@implementation MyInforChangeDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = @"修改手机号";
    [self.view addSubview:self.phoneLabel];
    [self.view addSubview:self.phoneNumber];
    [self.view addSubview:self.codeLabel];
    [self.view addSubview:self.code];
    [self.view addSubview:self.sureButton];
    
}

#pragma mark - GetCodeDelegate
- (void)LGetCodeWith:(LGetCodeView *)view click:(BOOL)click{
    
    
    /**  获取验证码  */
    
    if (self.phoneNumber.codeTextField.text.length == 0) {
        [YJProgressHUD showMessage:@"请输入手机号" inView:self.view];
        return;
    }

    NSString *first = [self.phoneNumber.codeTextField.text substringToIndex:1];
    if (self.phoneNumber.codeTextField.text.length != 11 || ![first isEqualToString:@"1"]) {
        [YJProgressHUD showMessage:@"手机号不符合规范" inView:self.view];
        return;
    }
    
    [self.code.codeButton startWithTime:59 title:@"获取验证码" countDownTitle:@"重新获取" mainColor:[UIColor whiteColor] countColor:[UIColor blackColor]];


    NSMutableDictionary *paramer = [NSMutableDictionary dictionary];
    paramer[@"MOBILE"] = self.phoneNumber.codeTextField.text; /**  这里输入的手机号  */
    paramer[@"CODE_SOURCE"] = @"upMobile";
    [NetWorkingManager postWithUrlString:[NetWorkingURL sendVeriCode] Parameter:paramer Success:^(id object) {
        if ([object[@"STATE"] isEqualToString:@"20000"]) {
        
        }
    } Fail:^(id object) {
        
    }];

}


/**  确认按钮的点击事件  */
- (void)clickButton{
    __weak typeof(self) weakSelf = self;
    if (self.phoneNumber.codeTextField.text.length == 0) {
        [YJProgressHUD showMessage:@"手机号不能为空" inView:self.view];
        return;
    }

    NSString *first = [self.phoneNumber.codeTextField.text substringToIndex:1];
    if (self.phoneNumber.codeTextField.text.length != 11 || ![first isEqualToString:@"1"]) {
        [YJProgressHUD showMessage:@"手机号不符合规范" inView:self.view];
        return;
    }

    if (self.code.codeTextField.text.length != 6) {
        [YJProgressHUD showMessage:@"请输入正确的验证码" inView:self.view];
        return;
    }

    
    NSMutableDictionary *paramer = [NSMutableDictionary dictionary];
    paramer[@"ACCOUNT_ID"] = ZJUSERINFORMODEL.ORG_ACCOUNT.ID;
    paramer[@"MOBILE"] = self.phoneNumber.codeTextField.text;
    paramer[@"MOBILE_CODE"] = self.code.codeTextField.text;
    paramer[@"CODE_SOURCE"] = @"upMobile";
    [YJProgressHUD showMessage:@"保存中..." duration:10 view:self.view];
    [NetWorkingManager postWithUrlString:[NetWorkingURL getUpdateUser] Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            [YJProgressHUD showMessage:@"保存成功" inView:weakSelf.view];
            
            /**  更新用户信息  */
            [ZJUserInfoModel sharedUserInfo].ORG_ACCOUNT.MOBILE = weakSelf.phoneNumber.codeTextField.text;
            [ZJUserInfoModel updateUserInfoToNSUserDefaults];
            
            /**  发送通知  */
            [[NSNotificationCenter defaultCenter] postNotificationName:@"sendPhoneNumber" object:weakSelf userInfo:@{@"phone": weakSelf.phoneNumber.codeTextField.text}];
            
            /**  修改成功回到我的信息界面  */
            [weakSelf.navigationController popToViewController:weakSelf.navigationController.viewControllers[1] animated:YES];

        }else if([model.STATE isEqualToString:@"30002"]){
            [YJProgressHUD showMessage:@"验证码错误" inView:weakSelf.view];
           
        }else if([model.STATE isEqualToString:@"60005"]){
            [YJProgressHUD showMessage:@"手机号更新失败" inView:weakSelf.view];
            
        }else{
            [YJProgressHUD showMessage:@"手机号更新失败" inView:weakSelf.view];

        }
        
    } Fail:^(id object) {
        [YJProgressHUD showMessage:@"手机号更新失败" inView:weakSelf.view];

    }];
    
    
}

#pragma mark - getter
- (UILabel *)phoneLabel{
    if (!_phoneLabel) {
        _phoneLabel = ({
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20 * [UIScreen zj_screenWScale], LP_TopHeight + 40 * [UIScreen zj_screenHScale], 100 * [UIScreen zj_screenWScale], 30 * [UIScreen zj_screenHScale])];
            label.textAlignment = NSTextAlignmentLeft;
            label.text = @"新手机号:";
            label.textColor = [UIColor textBlackColor];
            label.backgroundColor = [UIColor clearColor];
            label.font = [UIFont systemFontOfSize:17 * [UIScreen zj_textFontScale]];
            label;
        });
    }
    return _phoneLabel;
}

- (LGetCodeView *)phoneNumber{
    if (!_phoneNumber) {
        _phoneNumber = [[LGetCodeView alloc] initWithFrame:CGRectMake(40 * [UIScreen zj_screenWScale], CGRectGetMaxY(self.phoneLabel.frame) + 10 * [UIScreen zj_screenHScale], [UIScreen zj_screenWith] - 60 * [UIScreen zj_screenWScale], 40 * [UIScreen zj_screenHScale])];
        _phoneNumber.codeButton.hidden = YES;
        _phoneNumber.codeTextField.placeholder = @"输入手机号";
        _phoneNumber.maxNumber = 11;
    }
    return _phoneNumber;
}

- (UILabel *)codeLabel{
    if (!_codeLabel) {
        _codeLabel = ({
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20 * [UIScreen zj_screenWScale], CGRectGetMaxY(self.phoneNumber.frame) + 20 * [UIScreen zj_screenHScale], [UIScreen zj_screenWith] - 60 * [UIScreen zj_screenWScale], 30 * [UIScreen zj_screenHScale])];;
            label.textAlignment = NSTextAlignmentLeft;
            label.textColor = [UIColor textBlackColor];
            label.text = @"验证码:";
            label.backgroundColor = [UIColor clearColor];
            label.font =[UIFont systemFontOfSize:17 * [UIScreen zj_textFontScale]];
            label;
        });
    }
    return _codeLabel;
}


- (LGetCodeView *)code{
    if (!_code) {
        _code = [[LGetCodeView alloc] initWithFrame:CGRectMake(40 * [UIScreen zj_screenWScale], CGRectGetMaxY(self.codeLabel.frame) + 10 * [UIScreen zj_screenHScale], [UIScreen zj_screenWith] - 60 * [UIScreen zj_screenWScale], 40 * [UIScreen zj_screenHScale])];
        _code.delegate = self;;
    }
    return _code;
}



- (UIButton *)sureButton{
    if (!_sureButton) {
        _sureButton = ({
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(50 * [UIScreen zj_screenWScale], CGRectGetMaxY(self.code.frame) + [UIScreen zj_screenHScale] * 50, [UIScreen zj_screenWith] - [UIScreen zj_screenWScale] * 100, 50 * [UIScreen zj_screenHScale])];;
            [button setTitle:@"确认" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            button.layer.cornerRadius = 5;
            button.layer.masksToBounds = YES;
            button.backgroundColor = [UIColor themeColor];
            [button addTarget:self action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside];
            button.tag = 1;
            button;
            
        });
    }
    return _sureButton;
}

@end
